/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import src.domain.gl.Glaccnt;
import src.domain.saving.Savconfig;
import src.domain.system.Company;
import src.services.gl.GlaccntModel;
import src.services.saving.SavconfigModel;
import src.services.saving.SavprodModel;
import src.services.system.CompanyModel;
import src.util.Messager;

/**
 *
 * @author Lalugina
 */
@ManagedBean
//@RequestScoped
@SessionScoped
public class SavconfigBean implements Serializable {

    /**
     * Creates a new instance of SavconfigBean
     */
    
    private Savconfig savconfig;
    private List<SelectItem> avblGL, avblProd;
    private boolean editMode = true;
    private String compName;
    
    private BigDecimal maxTaxFreeBalance;
    private BigDecimal maxTaxFreeInterest;
    
    public SavconfigBean() {
        this.avblGL = new GlaccntModel().listAll();
        this.avblProd = new SavprodModel().listAll();
        
        Company company = new CompanyModel().getCompany();        
        this.savconfig = new SavconfigModel().getSavconfig();
        if (this.savconfig==null) {
            this.savconfig = new Savconfig();
            this.savconfig.setCompanyId(company.getCompanyId());
            this.editMode = false;
            
            maxTaxFreeBalance = BigDecimal.ZERO;
            maxTaxFreeInterest = BigDecimal.ZERO;
            
        } else {
            this.editMode = true;
             maxTaxFreeBalance =savconfig.getMaxTaxFreeBalance();
            maxTaxFreeInterest = savconfig.getMaxTaxFreeInterest();
            
        }
        
        this.compName = company.getCompanyName();        
        
    }

    public Savconfig getSavconfig() {
        return savconfig;
    }

    public void setSavconfig(Savconfig savconfig) {
        this.savconfig = savconfig;
    }

    public List<SelectItem> getAvblGL() {
        return avblGL;
    }

    public List<SelectItem> getAvblProd() {
        return avblProd;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public String getCompName() {
        return compName;
    }

    public BigDecimal getMaxTaxFreeBalance() {
        return maxTaxFreeBalance;
    }

    public void setMaxTaxFreeBalance(BigDecimal maxTaxFreeBalance) {
        this.maxTaxFreeBalance = maxTaxFreeBalance;
    }

    public BigDecimal getMaxTaxFreeInterest() {
        return maxTaxFreeInterest;
    }

    public void setMaxTaxFreeInterest(BigDecimal maxTaxFreeInterest) {
        this.maxTaxFreeInterest = maxTaxFreeInterest;
    }
    
    public void save() {
              
        boolean success = new SavconfigModel().saveData(savconfig, editMode);
        
        if (!success && !editMode) editMode=false;
        else editMode=true;
                
        Messager.showSaveUpdateStatus(success);     
        
        RequestContext.getCurrentInstance().addCallbackParam("success", success); 
    }
    
}
